{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# PaddleOcr 实现车牌识别\n",
    "\n",
    "用PaddleOcr模型实现车牌识别\n",
    "\n",
    "# 一、项目背景\n",
    "车牌识别广泛运用于例如停车场、收费站等等场景，提供高效快捷的车辆认证模型。\n",
    "=》检测图片的文本位置\n",
    "=》识别文本信息\n",
    "\n",
    "# 二、数据集简介\n",
    "本次使用的数据集为CCPD2019车牌数据集\n",
    "挑选了18张不同角度、采光、距离、清晰度的摄像头进行训练\n",
    "\n",
    "## 1.数据加载和预处理\n",
    "\n",
    "```python\n",
    "!pip install paddlepaddle==2.1.1\n",
    "!pip install paddleocr==2.0.2 paddlehub==2.0.4\n",
    "import matplotlib.pyplot as plt \n",
    "import matplotlib.image as mpimg \n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "## 2.数据集查看\n",
    "\n",
    "```python\n",
    "!cat test.txt\n",
    "with open('test.txt', 'r') as f:\n",
    "    test_img_path=[]\n",
    "    for line in f:\n",
    "        test_img_path.append(line.strip())\n",
    "print(test_img_path)\n",
    "```\n",
    "\n",
    "\n",
    "# 三、模型选择和开发\n",
    "\n",
    "典型的OCR技术路线如下图所示：\n",
    "\n",
    "\n",
    "\n",
    "其中OCR识别的关键路径在于文字检测和文本识别部分，这也是深度学习技术可以充分发挥功效的地方。PaddleHub为大家开源的预训练模型的网络结构是Differentiable Binarization+ CRNN，基于icdar2015数据集下进行的训练。\n",
    "\n",
    "首先，DB是一种基于分割的文本检测算法。在各种文本检测算法中，基于分割的检测算法可以更好地处理弯曲等不规则形状文本，因此往往能取得更好的检测效果。但分割法后处理步骤中将分割结果转化为检测框的流程复杂，耗时严重。因此作者提出一个可微的二值化模块（Differentiable Binarization，简称DB），将二值化阈值加入训练中学习，可以获得更准确的检测边界，从而简化后处理流程。DB算法最终在5个数据集上达到了state-of-art的效果和性能。参考论文：Real-time Scene Text Detection with Differentiable Binarization\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "接着，我们使用 CRNN（Convolutional Recurrent Neural Network）即卷积递归神经网络，是DCNN和RNN的组合，专门用于识别图像中的序列式对象。与CTC loss配合使用，进行文字识别，可以直接从文本词级或行级的标注中学习，不需要详细的字符级的标注。参考论文:An end-to-end trainable neural network for image-based sequence recognition and its application to scene text recognition\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 1.预训练模型\n",
    "\n",
    "```python\n",
    "\n",
    "import paddlehub as hub\n",
    "\n",
    "ocr = hub.Module(name=\"chinese_ocr_db_crnn_mobile\")\n",
    "!pip install shapely\n",
    "!pip install pyclipper\n",
    "import cv2\n",
    "\n",
    "```\n",
    "\n",
    "## 2.读取测试文件夹test.txt中的照片路径\n",
    "\n",
    "\n",
    "```python\n",
    "np_images =[cv2.imread(image_path) for image_path in test_img_path] \n",
    "\n",
    "\n",
    " ```\n",
    "        \n",
    "\n",
    "## 3.训练模型\n",
    "\n",
    "\n",
    "```python\n",
    "results = ocr.recognize_text(\n",
    "                    images=np_images,         # 图片数据，ndarray.shape 为 [H, W, C]，BGR格式；\n",
    "                    use_gpu=False,            # 是否使用 GPU；若使用GPU，请先设置CUDA_VISIBLE_DEVICES环境变量\n",
    "                    output_dir='ocr_result',  # 图片的保存路径，默认设为 ocr_result；\n",
    "                    visualization=True,       # 是否将识别结果保存为图片文件；\n",
    "                    box_thresh=0.5,           # 检测文本框置信度的阈值；\n",
    "                    text_thresh=0.5)          # 识别中文文本置信度的阈值；\n",
    "\n",
    "for result in results:\n",
    "    data = result['data']\n",
    "    save_path = result['save_path']\n",
    "    for infomation in data:\n",
    "         print('text: ', infomation['text'], '\\nconfidence: ', infomation['confidence'], '\\ntext_box_position: ', infomation['text_box_position'])\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "# 四、调参优化\n",
    "套件：paddleocr\n",
    "\n",
    "优化器：Momentum\n",
    "\n",
    "调整参数：增加了 warmup_steps 100 同时增加了steps 到1000 在加入了warmip_steps后 准确率略有上升。 原始的steps为100 在增加到1000后发现 图片识别的准确率下降速率减缓 单个图片的识别率略有提升\n",
    "\n",
    "心得：\n",
    "\n",
    "# 五、总结与升华\n",
    "\n",
    "实际运用了paddle相关套件 熟悉了使用paddledetection来搭建任务代码 对于SDG和Momentum优化器的区别以及各自的优势进行了学习 对于学习的参数 例如基本学习速率等有了实际的运用以及调整 能够通过函数输出的结果进行调整参数以及 增加参数以达到更高的识别率\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.1.0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
